package com.qingzhou.web.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.solon.service.impl.ServiceImpl;
import com.qingzhou.web.repository.dao.MaterialsDao;
import com.qingzhou.web.repository.entity.MaterialsEntity;
import com.qingzhou.web.repository.mapper.MaterialsMapper;
import com.qingzhou.web.service.ConvertHelper;
import com.qingzhou.web.service.MaterialsService;
import com.qingzhou.web.service.impl.params.request.*;
import com.qingzhou.web.service.impl.params.vo.MaterialsVo;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;

@Component
public class MaterialsServiceImpl extends ServiceImpl<MaterialsMapper, MaterialsEntity> implements MaterialsService {
    @Inject
    MaterialsDao materialsDao;
    @Override
    public int add(MaterialsAddForm form) {
        MaterialsEntity entity = BeanUtil.copyProperties(form, MaterialsEntity.class);
        return materialsDao.add(entity);
    }

    @Override
    public int delete(MaterialsDeleteForm form) {
        MaterialsEntity entity = materialsDao.getById(form.getId());
        Assert.isTrue(ObjUtil.isNotNull(entity),"数据不见了");

        return materialsDao.delete(form.getId());
    }

    @Override
    public Page<MaterialsEntity> page(Page<MaterialsEntity> page, MaterialsEntity entity) {
        return materialsDao.getMapper().paginate(page, QueryWrapper.create(entity)
                .orderBy("sort"));
    }

    @Override
    public MaterialsEntity detail(MaterialsDetailForm form) {
        MaterialsEntity entity = materialsDao.getById(form.getId());
        Assert.isTrue(ObjUtil.isNotNull(entity),"数据不见了");
        return entity;
    }

    @Override
    public int edit(MaterialsEditForm form) {
        MaterialsEntity entity = materialsDao.getById(form.getId());
        Assert.isTrue(ObjUtil.isNotNull(entity),"数据不见了");

        BeanUtil.copyProperties(form,entity);
        return materialsDao.update(entity);
    }

}
